//
// Created by tanzx on 12/21/0021.
//
#include <iostream>
#include <csignal>
#include <cstdio>
#include <sys/types.h>
#include <unistd.h>
#include <thread>

int test_process() {
    std::cout << "PID: " << getpid() << std::endl;
    std::cout << "Thread ID: " << std::this_thread::get_id() << std::endl;
    pid_t pid = fork();
    if (pid < 0) {
        std::cerr << "Failed to create a new process." << std::endl;
        return 1;
    } else if (pid == 0) {
        std::cout << std::endl;
        std::cout << "child process pid = "<< pid << ",This is the child process." << std::endl;
        std::cout << "child process's Thread ID: " << std::this_thread::get_id() << std::endl;
        std::cout << "child process's PID: " << getpid() << std::endl;
        // 这里是子进程的代码
        return 0;
    } else {
        std::cout << std::endl;
        std::cout << "parent process pid = "<< pid <<  ",This is the parent process." << std::endl;
        std::cout << "parent process's Thread ID: " << std::this_thread::get_id() << std::endl;
        std::cout << "parent process's PID: " << getpid() << std::endl;
        // 等待 3 秒
        sleep(3);
        // 杀死子进程
//        kill(pid, SIGTERM);
//        std::cout << "Child process has been killed." << std::endl;
        return 0;
    }
}